local Msg = require("core.Msg")
local ExcelMap = require("excel.Map")
local CopyConfig = ExcelMap.copy
local CopyDefine = require("copy.Define")
local Lang = require("common.Lang")
local TeamLogic = require("team.TeamLogic")
local Broadcast = require("broadcast.Broadcast")
local Human = require("core.Human")
local CopyW = require("copy.CopyW")
local InnerDataManager = require("innerdata.InnerDataManagerFW")
local InnerDefine = require("innerdata.Define")
local CallbackW = require("copy.CallbackW")
local CopyTeamSign = require("copy.CopyTeamSignW")
local CopyCommon = require("copy.CopyCommon")
local MiddleCopySign = require("middle.MiddleCopySign")



--玩家跨线程申请副本
function LW_CROSS_ROOM_APPLY(human, mm)
   if human.fd == nil then
      return
   end
   CopyW.applyCopyRoom(human, mm.copyid, mm.startTime)
end

function LW_CROSS_ROOM_APPLY_RESULT(human, mm)
  human = Human.onlineUuid[mm.uuid]
  if human and human.fd == nil then
      print("LW_CROSS_ROOM_APPLY_RESULT........fuck:",human._id)
      return
  end
  CopyW.finishApplyCopyRoom(mm.uuid, human, mm.copyid, mm.roomid)
end


--正确切换线程，需要到world里来设置fd logic表
function LW_CROSS_COPY_ROOM_ENTER(human, mm)
  if human.fd == nil then
      return
  end
  Msg.set_logic(human.fd, human._id, CopyW.getCopyLogicID(mm.copyid))    
  local msg = Msg.wl.WL_CROSS_COPY_ROOM_ENTER
  msg.copyid  = mm.copyid
  msg.roomid  = mm.roomid
  msg.id  = human.id
  msg.uid = human.uid  
  msg.fd = human.fd
  msg.login = human.loginFinish and 0 or 1
  Msg.send2logic(msg, human._id)    
end

function CG_COPY_TEAM_SIGN(human, mm)    
  if MiddleCopySign.checkCanSignMiddle(mm.copyID) then --如果能跨服，转到跨服去匹配
        MiddleCopySign.sign(human, mm.copyID)
        return    
  end
  CopyTeamSign.sign(human, mm.copyID)  
end
function CG_COPY_TEAM_READY(human, mm)
  if MiddleCopySign.checkCanSignMiddle(mm.copyID) then --如果能跨服，转到跨服去匹配
        MiddleCopySign.signReady(human, mm.copyID)
        return    
  end
  CopyTeamSign.signReady(human, mm.copyID)
end
function CG_COPY_TEAM_SIGN_EXIT(human, mm)
  if MiddleCopySign.checkCanSignMiddle(mm.copyID) then --如果能跨服，转到跨服去匹配
        MiddleCopySign.signExit(human, mm.copyID)
        return    
  end
  CopyTeamSign.signExit(human, mm.copyID)
end

function CG_COPY_TEAM_MATCH_AD(human, mm)
  if MiddleCopySign.checkCanSignMiddle(mm.copyID) then --如果能跨服，转到跨服去匹配
        MiddleCopySign.ad(human, mm.copyID)
        return
  end
  CopyTeamSign.ad(human, mm.copyID)
end
function CG_COPY_TEAM_SIGN_JOIN(human, mm)
  if MiddleCopySign.checkCanSignMiddle(mm.copyID) then
        MiddleCopySign.sign(human, mm.copyID)
        return
  end
  CopyTeamSign.signJoin(human, mm.uuid, mm.copyID, mm.uid)  
end

function CG_COPY_TEAM_LEADER_ENTER(human, mm)
  local copyConfig = CopyConfig[mm.copyID]
  if not copyConfig or copyConfig.type ~= CopyDefine.COPY_TYPE_TEAM then
      return
  end
  local team = TeamLogic.getTeamByUuid(human._id) 
  if team then 
      local leaderUuid = TeamLogic.getLeaderUuid(team)
      if leaderUuid ~= human._id then -- 只有队长才能申请
        Broadcast.sendBroadcast(human, Lang.MOUSE+Lang.TEAM, Lang.COPY_ENTER_ERR_NO_TEAMLEADER)
        return
      end
      
      for i = 1, TeamLogic.TEAM_MAX_MEMBER_COUNT do
          if team[i] ~= nil and Human.onlineUuid[team[i].uuid] ~= nil and Human.onlineUuid[team[i].uuid].fd then
            local err = CopyCommon.checkEnterCondition(Human.onlineUuid[team[i].uuid], mm.copyID) 
            if err then
              if i > 1 then
                  err = "您的队友"..err
              end
              Broadcast.sendBroadcast(human, Lang.MOUSE, err)  
              return
            end
          end
      end
  else
      local err = CopyCommon.checkEnterCondition(human, mm.copyID) 
      if err then
          Broadcast.sendBroadcast(human, Lang.MOUSE, err)  
          return
      end
  end
  
  CopyW.applyCopyRoom(human, mm.copyID, os.time())
end
function LW_COPY_CALLBACK(uuid, msg)
  local fun = CallbackW[msg.type]
  if fun then
      fun(msg.copyID, msg.roomID, msg.param)
  else
      print(" msg.type.....")
  end 
end
function onSendTeamError(human, type, data)
  local team = TeamLogic.getTeamByUuid(human._id) 
  if not team then
      return
  end
  local copyConfig = CopyConfig[data.copyID]
  if not copyConfig or copyConfig.type ~= CopyDefine.COPY_TYPE_TEAM then
      return
  end
  Broadcast.sendBroadcastFDs(TeamLogic.getTeamOnlineMemberFD(team), Lang.CHAT+Lang.TEAM, data.err)  
end
InnerDataManager.Register(InnerDefine.INNERDATA_TYPE_COPY_TEAM_ERR, onSendTeamError)

